package com.ygxsk.carhome.views.picker;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.response.LeadCarTypeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 普通的文字选择器,我自己改造的；直接的传递一个对象的
 * Created by ycuwq on 17-12-28.
 */
public class BankCustomPicker extends WheelPicker<LeadCarTypeBean> {

    //该界面的值内容；可以使从本地存储取出，也可以是直接传递过来的；
    private ArrayList<LeadCarTypeBean> mDataResources = new ArrayList<>();

    //设置当前选中的，供外界调用的,默认有一个值，防止对象为空值；
    private LeadCarTypeBean mSelectedNomal;

    private OnLeadTypeSelectedListener mOnLeadTypeSelectedListener;

    public BankCustomPicker(Context context) {
        this(context, null);
    }

    public BankCustomPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BankCustomPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //2020年01月03日是不是该在这里设置个性化内容?
        initAttrs(context, attrs);

        updateNomal();

        //设置一个默认选中的？
        setSelectedNomal(mSelectedNomal);

        setOnWheelChangeListener(new OnWheelChangeListener<LeadCarTypeBean>() {

            @Override
            public void onWheelSelected(LeadCarTypeBean itemBean, int position, String itemString) {
                //这个可以不要
                mSelectedNomal = itemBean;
                LogUtils.json("itchen--NomalPicker---onWheelSelected--选中的【字符】-->" + itemBean);

                if (mOnLeadTypeSelectedListener != null) {
                    mOnLeadTypeSelectedListener.onLeadTypeSelected(position, itemBean, itemString);
                }

            }
        });
    }

    private void initAttrs(Context context, AttributeSet attrs) {

        if (attrs == null) {
            return;
        }

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyNomalPicker);
        int textSize = a.getDimensionPixelSize(R.styleable.MyNomalPicker_itemTextSize, getResources().getDimensionPixelSize(R.dimen.WheelItemTextSize));
        int textColor = a.getColor(R.styleable.MyNomalPicker_itemTextColor, Color.BLACK);
        boolean isTextGradual = a.getBoolean(R.styleable.MyNomalPicker_textGradual, true);
        //boolean isCyclic = a.getBoolean(R.styleable.MyNomalPicker_wheelCyclic, false);
        int halfVisibleItemCount = a.getInteger(R.styleable.MyNomalPicker_halfVisibleItemCount, 2);
        int selectedItemTextColor = a.getColor(R.styleable.MyNomalPicker_selectedTextColor, getResources().getColor(R.color.com_ycuwq_datepicker_selectedTextColor));
        int selectedItemTextSize = a.getDimensionPixelSize(R.styleable.MyNomalPicker_selectedTextSize, getResources().getDimensionPixelSize(R.dimen.WheelSelectedItemTextSizesmall));//选中的文字的大小
        int itemWidthSpace = a.getDimensionPixelSize(R.styleable.MyNomalPicker_itemWidthSpace, getResources().getDimensionPixelOffset(R.dimen.WheelItemWidthSpace));
        int itemHeightSpace = a.getDimensionPixelSize(R.styleable.MyNomalPicker_itemHeightSpace, getResources().getDimensionPixelOffset(R.dimen.WheelItemHeightSpace));
        boolean isZoomInSelectedItem = a.getBoolean(R.styleable.MyNomalPicker_zoomInSelectedItem, true);
        boolean isShowCurtain = a.getBoolean(R.styleable.MyNomalPicker_wheelCurtain, true);
        int curtainColor = a.getColor(R.styleable.MyNomalPicker_wheelCurtainColor, Color.WHITE);
        boolean isShowCurtainBorder = a.getBoolean(R.styleable.MyNomalPicker_wheelCurtainBorder, true);
        //原始代码的分割线线条颜色？我还不确定
        int curtainBorderColor = a.getColor(R.styleable.MyNomalPicker_wheelCurtainBorderColor, getResources().getColor(R.color.com_ycuwq_datepicker_divider));
//        int curtainBorderColor = a.getColor(R.styleable.MyNomalPicker_wheelCurtainBorderColor, getResources().getColor(R.color.linenv));

        a.recycle();

        setTextSize(textSize);

        setTextColor(textColor);

        setTextGradual(isTextGradual);

//        isCyclic =false;//默认设置为是无限循环的

        setCyclic(false);

        setHalfVisibleItemCount(halfVisibleItemCount);

        setSelectedItemTextColor(selectedItemTextColor);

        setSelectedItemTextSize(selectedItemTextSize);

        setItemWidthSpace(itemWidthSpace);

        setItemHeightSpace(itemHeightSpace);

        setZoomInSelectedItem(isZoomInSelectedItem);

        setShowCurtain(isShowCurtain);

        setCurtainColor(curtainColor);

        setShowCurtainBorder(isShowCurtainBorder);

        setCurtainBorderColor(curtainBorderColor);

    }

    public void updateNomal() {

        //直接的设置值内容也能够实现；

        //直接从本地数据库取出？
        //从mmkv之中取出？ 保险业务排名-的【保险公司的排名】
        //ArrayList<SafetyCompanyInfoBean> company = MMKV.defaultMMKV().decodeString();
        String leadCarTypeInfoJson = MMKV.defaultMMKV().decodeString(ConstantSign.MMKV_JSON_CAR_TYPES_JSON);
        //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "--safetyCompanyInfoJson-->" + safetyCompanyInfoJson);
        ArrayList<LeadCarTypeBean> bankCompanyInfoBeans = (ArrayList<LeadCarTypeBean>) JSON.parseArray(leadCarTypeInfoJson, LeadCarTypeBean.class);

        if (bankCompanyInfoBeans != null && !bankCompanyInfoBeans.isEmpty()) {

            //这里打开，第一个默认值就是选中值；
            mSelectedNomal = bankCompanyInfoBeans.get(0);

            //装载的是真实对象，
            //List<Integer> list = new ArrayList<>();
            //装载那个对象对应的
            List<String> listString = new ArrayList<>();

            for (int index = 0; index < bankCompanyInfoBeans.size(); index++) {
                //list.add(companyInfoBeans.get(index).getCompanyId());
                //list.add(index);//设置对应的位置；
                listString.add(bankCompanyInfoBeans.get(index).getDictLabel());
            }

//            for (int i = mMinMonth; i <= mMaxMonth; i++) {
//                list.add(i);
//                //listString.add(String.format("%02d", i) + "月");
//                listString.add(String.format("%02d", i) + StringUtils.getString(R.string.unit_month));
//            }

            //最后添加一个：清空重置
//        list.add(13);
//        listString.add(StringUtils.getString(R.string.chooseseingnull));

            setDataList(bankCompanyInfoBeans, listString);

        } else {

            //直接获取接口取出数据？
        }
    }

    //设置一个默认的选中的 值内容
//    public void setSelectedNomal(SafetyCompanyInfoBean safetyCompanyInfoBeanSelect) {
//        setSelectedYear(safetyCompanyInfoBeanSelect, true);
//    }
//
//    public void setSelectedNomal(int selectedYear, boolean smoothScroll) {
//        setCurrentPosition(selectedYear - mStartYear, smoothScroll);
//    }


    public LeadCarTypeBean getSelectedNomal() {
        return mSelectedNomal;
    }

    public void setSelectedNomal(LeadCarTypeBean selectedNomal) {
        this.mSelectedNomal = selectedNomal;
    }

    //    public ArrayList<SafetyCompanyInfoBean> getDataResources() {
//        return mDataResources;
//    }
//
    public void setDataResources(ArrayList<LeadCarTypeBean> _dataResources) {
        this.mDataResources = _dataResources;
    }

    public void setOnLeadTypeSelectedListener(OnLeadTypeSelectedListener _onLeadTypeSelectedListener) {
        mOnLeadTypeSelectedListener = _onLeadTypeSelectedListener;
    }

    public interface OnLeadTypeSelectedListener {
        void onLeadTypeSelected(int _indexNomal, LeadCarTypeBean _itemBean, String _nomalString);
    }

}
